package calculateDistance;

import java.util.ArrayList;

import pointAndLine.Point;

public class LineEquation {
	private double steep;
	private double freeTerm;
	private double alfa;
	private double beta;
	private double a,b,c;
	private double distance;
	private double thisX,thisY;
	private boolean inverted;
	
	public LineEquation(double dist)
	{
		distance=dist;
	}
	
	public double getDistance() {
		return distance;
	}

	public void setDistance(double distance) {
		this.distance = distance;
	}

	public void calculateValues(Point p1, Point p2)
	{
		double upper=(p2.getY()-p1.getY());
		double lower=(p2.getX()-p1.getX());
		if (lower!=0)
			steep=upper/lower;
		else steep=0.0;
		freeTerm=steep*p1.getX()-p1.getY();
	}
	
	public void invert()
	{
		double aux=thisX;
		thisX=thisY;
		thisY=aux;
		inverted=false;
	}
	
	public boolean verifyEquation(Point p)
	{
		boolean answer=false;
		inverted=true;
		System.out.println("steep: " +steep + " freeterm: "+ freeTerm);
		thisX=p.getX();
		thisY=p.getY();
		if (steep==0.0) invert();
		double result=thisY-this.steep*thisX+this.freeTerm;
		System.out.println(result);
		if (result==0.0) answer=true;
		return answer;
	}
	
	public void setValues(Point p)
	{
		alfa=thisY;
		beta=thisX;
		a=1+steep*steep;
		b=(-2)*steep*freeTerm-2*beta-2*steep*alfa;
		c=freeTerm*freeTerm+2*freeTerm*alfa+alfa*alfa+beta*beta-distance*distance;		
	}
	
	public ArrayList<Point> solveGrade2Equation()
	{
		System.out.println(a +"  "+ "  "+b+ "  "+c );
		ArrayList<Point> result=new ArrayList<Point>();
		double delta=b*b-4*a*c;
		double y1=Math.abs((-b+delta)/(2*a));
		double y2=Math.abs((-b-delta)/(2*a));
		double x1=Math.abs(steep*y1-freeTerm);
		double x2=Math.abs(steep*y2-freeTerm);
		System.out.println(x1+" " +y1 + "   --   " + x2 + " "+ y2);
		if (inverted==true){
		result.add(new Point(y1,x1));
		result.add(new Point(y2,x2));}
		else {
			result.add(new Point(x1,y1));
			result.add(new Point(x2,y2));
		}
		return result;
	}
}
